package Controller;

import java.awt.Point;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

import Main.FlummoxMain;
import Model.*;
import View.GraphicalView;

/*
*Menuhandler handles all menubar input from the user
* -Commented(Apr 4,2011)
* Samual Martelli
* </pre>
*/
public class MenuHandler implements ActionListener {

	private JMenuItem menuItemHandled;
	private GraphicalView menuView;
	private GameAI gameAI;
	public MenuHandler(JMenuItem menuItem,GraphicalView view){
		menuItemHandled = menuItem;
		menuView = view;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(menuItemHandled.getText() == GraphicalView.PACMAN){
			launch(Game.PACMAN_GAME);
		}
		else if(menuItemHandled.getText() == GraphicalView.BUNNY){
			launch(Game.BUNNY_GAME);
		}
		else if(menuItemHandled.getText() == GraphicalView.MOUSE){
			launch(Game.MOUSE_GAME);
		}
		else if(menuItemHandled.getText() == GraphicalView.RESET){
			if (gameAI != null){
				launch(gameAI.getGame().getGameType());
			}
		}

	}
	

	public void launch(int gameType) {
		//remove all previous observers
		if(gameAI!= null){
			gameAI.getGame().getData().getPlayerOfGame().deleteObserver(menuView);
			for(Monster m:gameAI.getGame().getData().getMonsters()){
				m.deleteObserver(menuView);
			}
		}
		switch (gameType) {
		case Game.PACMAN_GAME:	
			//Initialising
			Maze m = new Maze(Game.PACMAN_GAME, Maze.MAP_SIZE);			
			ArrayList<Monster> monsters = new ArrayList<Monster>();
			PacMan p = new PacMan(Maze.PLAYER_START);
			Ghost rand = new Ghost(Maze.MONSTER_POS1, GameAI.RANDOM_MOVE);
			Ghost chaser = new Ghost(Maze.MONSTER_POS2, GameAI.CHASE_MOVE);
			Ghost fraid = new Ghost(Maze.MONSTER_POS3, GameAI.FLEEING_MOVE);
			monsters.add(rand);
			monsters.add(chaser);
			monsters.add(fraid);
			p.addObserver(menuView);
			for( Monster gm:monsters){
				gm.addObserver(menuView);
			}
			GameData newData = new GameData(p,monsters, m);
			Game g = new Game(Game.PACMAN_GAME, newData);
			gameAI  = new GameAI(Game.PACMAN_GAME,g); 
			for (int i = 0; i < Maze.MAP_SIZE; i++) {
				for (int j = 0; j < Maze.MAP_SIZE; j++) {
					Point here = new Point(i, j);

					if (gameAI.getGame().getData().isPlayerAt(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.PLAYER_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.pacIcon);
					} else if (gameAI.getGame().getData().isMonsterAt(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.MONSTER_SPACE);
						menuView.getButton(i, j).setEnabled(true);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.ghostIcon);
					} else if (!gameAI.getGame().getData().getGameMaze().isAPortal(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.PORTAL_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.portalIcon);
					}
					// if a wall is here
					else if (!gameAI.getGame().getData().getGameMaze().isAWall(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.WALL_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.wallIcon);
					}
					else{
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.ITEM_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.dotIcon);
					}

				}
			}
			break; //get out of the switch here
		case Game.BUNNY_GAME:
			//Initialising
			Maze m2 = new Maze(Game.BUNNY_GAME, Maze.MAP_SIZE);			
			ArrayList<Monster> monsters2 = new ArrayList<Monster>();
			FriskyBunny f = new FriskyBunny(Maze.PLAYER_START);
			Fox fox = new Fox(Maze.MONSTER_POS1, GameAI.CHASE_MOVE);
			RunningBunny rb = new RunningBunny(Maze.RUNNINGBUNNY_POS, GameAI.FLEEING_MOVE);
			monsters2.add(fox);
			monsters2.add(rb);
			f.addObserver(menuView);
			for( Monster gm:monsters2){
				gm.addObserver(menuView);
			}
			GameData newData2 = new GameData(f,monsters2, m2);
			Game g2 = new Game(Game.BUNNY_GAME, newData2);
			gameAI  = new GameAI(Game.BUNNY_GAME,g2); 
			for (int i = 0; i < Maze.MAP_SIZE; i++) {
				for (int j = 0; j < Maze.MAP_SIZE; j++) {
					Point here = new Point(i, j);

					if (gameAI.getGame().getData().isPlayerAt(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.PLAYER_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.friskyIcon);
					} else if (gameAI.getGame().getData().isMonsterAt(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.MONSTER_SPACE);
							//fox's start
							if(here.equals(Maze.MONSTER_POS1)){
								menuView.changeButtonIcon(here.x, here.y,
										GraphicalView.foxIcon);
							}
							else if(here.equals(Maze.RUNNINGBUNNY_POS)){
								menuView.changeButtonIcon(here.x, here.y,
										GraphicalView.runIcon);
							}
							
						
						
					} 
					// if a wall is here change it to hedge for the bunny game
					else if (!gameAI.getGame().getData().getGameMaze().isAWall(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.HEDGE_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.hedgeIcon);
					}
					else{
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.EMPTY_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.noIcon);
					}
				}
			}
            break;
		case Game.MOUSE_GAME:
			//Initialising
			Maze m3 = new Maze(Game.MOUSE_GAME, Maze.MAP_SIZE);			
			ArrayList<Monster> monsters3 = new ArrayList<Monster>();
			TinyTim  t = new TinyTim(Maze.PLAYER_START);
			Rat rand3 = new Rat(Maze.MONSTER_POS1, GameAI.RANDOM_MOVE);
			Rat chaser3 = new Rat(Maze.MONSTER_POS2, GameAI.CHASE_MOVE);
			Rat fraid3 = new Rat(Maze.MONSTER_POS3, GameAI.FLEEING_MOVE);
			monsters3.add(rand3);
			monsters3.add(chaser3);
			monsters3.add(fraid3);
			t.addObserver(menuView);
			for( Monster gm:monsters3){
				gm.addObserver(menuView);
			}
			GameData newData3 = new GameData(t,monsters3, m3);
			Game g3 = new Game(Game.MOUSE_GAME, newData3);
			gameAI  = new GameAI(Game.MOUSE_GAME,g3); 
			for (int i = 0; i < Maze.MAP_SIZE; i++) {
				for (int j = 0; j < Maze.MAP_SIZE; j++) {
					Point here = new Point(i, j);

					if (gameAI.getGame().getData().isPlayerAt(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.PLAYER_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.timIcon);
					} else if (gameAI.getGame().getData().isMonsterAt(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.MONSTER_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.ratIcon);
					} else if (!gameAI.getGame().getData().getGameMaze().isAPortal(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.RAT_TUNNEL_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.ratTunnelIcon);
					}
					// if a wall is here
					else if (!gameAI.getGame().getData().getGameMaze().isAWall(here)) {
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.WALL_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.wallIcon);
					}
					else if(here.equals(Maze.EXIT)){
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.EXIT_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.exitIcon);
					}
					else{
						gameAI.getGame().getData().getGameMaze().setMazeItem(here,
								Maze.EMPTY_SPACE);
						menuView.changeButtonIcon(here.x, here.y,
								GraphicalView.noIcon);
					}

				}
			}
			break; //get out of the switch here
		}
		for (int x = 0; x < Maze.MAP_SIZE; x++) {
			for (int y = 0; y < Maze.MAP_SIZE; y++) {
				//instantiate the other controller here
				ButtonHandler curButtonHandler = new ButtonHandler
				(menuView.getButton(x, y),menuView,gameAI);
				menuView.getButton(x, y).addActionListener(curButtonHandler);
			}
		}
	}


}
